#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlTableModel>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	m_db = QSqlDatabase::addDatabase("QSQLITE");
	m_model = new QSqlTableModel(this, m_db);
	m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
	ui->tableView->setModel(m_model);
	ui->tableView->horizontalHeader()->setStretchLastSection(true);
	ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(ui->tableView,&QTableView::customContextMenuRequested,this,[this](QPoint pos){
		auto index =ui->tableView->indexAt(pos);
		if(index.isValid())
		{
			ui->menuEdit->exec(QCursor::pos());
		}

	});
}

MainWindow::~MainWindow()
{
	delete ui;
}

bool MainWindow::createDatabase(const QString &path)
{
	if (m_db.isOpen())
		m_db.close();
	m_db.setDatabaseName(path);

	bool ok = m_db.open();
	if (ok)
	{
		QSqlQuery query;
		query.exec("drop table if exists student");
		ok = query.exec("create table  student (id integer  primary key autoincrement,name text,score real)");
		if (ok)
		{
			query.exec("select * from student");
			if (!query.next())
			{
				query.exec("insert into  student (name,score) values ('小张',100)");
				query.exec("insert into  student (name,score) values ('李华',100)");
				query.exec("insert into  student (name,score) values ('小红',100)");
			}
		}
		else
		{
			qDebug() << query.lastError();
		}
	}
	if (!ok)
		qDebug() << m_db.lastError();
	return ok;
}
void MainWindow::on_actNewTem_triggered()
{
	auto savePath = QFileDialog::getSaveFileName(this, "新建数据", "./", "database (*.db)");
	if (!savePath.isEmpty() && createDatabase(savePath))
		initView();
	else
		QMessageBox::warning(this, "错误", "创建数据库失败");
}
bool MainWindow::openDB(const QString &path)
{
	if (m_db.isOpen())
		m_db.close();
	m_db.setDatabaseName(path);
	if (!m_db.open())
	{

		qDebug() << m_db.lastError();
		return false;
	}
	return true;
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
	m_model->clear();
	m_model->setTable(ui->comboBox->currentText());
	m_model->select();
}
void MainWindow::on_actOpen_triggered()
{
	auto path = QFileDialog::getOpenFileName(this, "选择数据库", "./", "database (*.db)");
	if (!path.isEmpty() &&openDB(path))
		initView();
	else
		QMessageBox::warning(this, "错误", "打开数据失败");
}

void MainWindow::initView()
{
	if (m_db.isOpen())
	{
		auto tableList = m_db.tables();
		if (tableList.length() > 1)
		{
			ui->comboBox->clear();
			ui->comboBox->addItems(tableList);
			ui->comboBox->setCurrentIndex(0);
			// ui->comboBox->removeItem(ui->comboBox->size()-1);//删除最后默认的书库
		}
	}
}
void MainWindow::on_pbtnCommit_clicked()
{
	m_model->database().transaction();
	if (m_model->submitAll())
		m_model->database().commit();
	else
	{
		QMessageBox::information(this, "提示", "保存失败");
		m_model->database().rollback();
	}
}
void MainWindow::on_pbtnRevert_clicked()
{
	m_model->revertAll();
}
void MainWindow::on_actDel_triggered()
{
	m_model->removeRow(ui->tableView->currentIndex().row());
}
void MainWindow::on_actInsert_triggered()
{
	int row=m_model->rowCount();
	m_model->insertRow(row);
	ui->tableView->selectRow(row);
}